Skip to content

Commit 1f9805a

Browse files
committed
Polish code
1 parent 794b06a commit 1f9805a

File tree

7 files changed

+280
-1
lines changed

7 files changed

+280
-1
lines changed

projects/stage-1/shopizer/sc-sm-shop/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
<dependencies>
1919

20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
2025
<!-- <dependency>-->
2126
<!-- <groupId>org.springframework.cloud</groupId>-->
2227
<!-- <artifactId>spring-cloud-config-client</artifactId>-->
@@ -46,6 +51,12 @@
4651
<artifactId>spring-cloud-starter-openfeign</artifactId>
4752
</dependency>
4853

54+
<!-- Spring Cloud Sleuth -->
55+
<dependency>
56+
<groupId>org.springframework.cloud</groupId>
57+
<artifactId>spring-cloud-starter-sleuth</artifactId>
58+
</dependency>
59+
4960
<!-- Testing -->
5061
<dependency>
5162
<groupId>org.springframework.cloud</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.salesmanager.shop.spring.cloud.service.client;
18+
19+
import org.springframework.cloud.openfeign.FeignClient;
20+
import org.springframework.web.bind.annotation.RequestMapping;
21+
22+
/**
23+
* TODO Comment
24+
*
25+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
26+
* @since TODO
27+
*/
28+
@FeignClient("service-provider")
29+
public interface HelloWorldService {
30+
31+
@RequestMapping("/hello/world")
32+
String helloWorld();
33+
}

projects/stage-1/shopizer/sc-sm-shop/src/main/java/com/salesmanager/shop/spring/cloud/service/client/ServiceClientApplication.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@
1616
*/
1717
package com.salesmanager.shop.spring.cloud.service.client;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.beans.factory.annotation.Autowired;
1922
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.builder.SpringApplicationBuilder;
24+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
2025
import org.springframework.cloud.openfeign.EnableFeignClients;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.web.bind.annotation.RequestMapping;
28+
import org.springframework.web.bind.annotation.RestController;
29+
import org.springframework.web.client.RestTemplate;
2130

2231
/**
2332
* TODO Comment
@@ -26,6 +35,41 @@
2635
* @since TODO
2736
*/
2837
@EnableAutoConfiguration
29-
@EnableFeignClients
38+
@EnableFeignClients(clients = HelloWorldService.class)
39+
@RestController
3040
public class ServiceClientApplication {
41+
42+
private static final Logger log = LoggerFactory.getLogger(ServiceClientApplication.class);
43+
44+
@Autowired
45+
private HelloWorldService helloWorldService;
46+
47+
@Autowired
48+
private RestTemplate restTemplate;
49+
50+
@RequestMapping("/")
51+
public String index() {
52+
String message = helloWorldService.helloWorld();
53+
log.info(message);
54+
return message;
55+
}
56+
57+
@RequestMapping("/rest")
58+
public String rest() {
59+
String message = restTemplate.getForObject("http://service-provider/hello/world", String.class);
60+
log.info(message);
61+
return message;
62+
}
63+
64+
@Bean
65+
@LoadBalanced
66+
public RestTemplate restTemplate() {
67+
return new RestTemplate();
68+
}
69+
70+
public static void main(String[] args) {
71+
new SpringApplicationBuilder(ServiceClientApplication.class)
72+
.run("--spring.config.additional-location=classpath:/META-INF/service-client.yaml");
73+
74+
}
3175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.salesmanager.shop.spring.cloud.service.provider;
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.cloud.sleuth.annotation.NewSpan;
22+
import org.springframework.stereotype.Service;
23+
24+
/**
25+
* Echo Service
26+
*
27+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
28+
* @since 1.0.0
29+
*/
30+
@Service
31+
public class EchoService {
32+
33+
private static final Logger log = LoggerFactory.getLogger(EchoService.class);
34+
35+
36+
@NewSpan
37+
public String echo(String message) {
38+
log.info(message);
39+
return "[ECHO] : " + message;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.salesmanager.shop.spring.cloud.service.provider;
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.builder.SpringApplicationBuilder;
24+
import org.springframework.cloud.sleuth.Tracer;
25+
import org.springframework.cloud.sleuth.annotation.NewSpan;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
28+
import org.springframework.web.bind.annotation.RequestMapping;
29+
import org.springframework.web.bind.annotation.RestController;
30+
31+
/**
32+
* TODO Comment
33+
*
34+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
35+
* @since TODO
36+
*/
37+
@EnableAutoConfiguration
38+
@RestController
39+
@EnableAspectJAutoProxy(proxyTargetClass = true)
40+
public class ServiceProviderApplication {
41+
42+
private static final Logger log = LoggerFactory.getLogger(ServiceProviderApplication.class);
43+
44+
@Autowired
45+
private Tracer tracer;
46+
47+
@Autowired
48+
private EchoService echoService;
49+
50+
51+
@RequestMapping("/")
52+
@NewSpan
53+
String home() {
54+
log.info("Hello,World");
55+
return echoService.echo("Hello world!");
56+
}
57+
58+
@RequestMapping("/hello/world")
59+
String helloWorld() {
60+
log.info("Hello world 2021!");
61+
return echoService.echo("Hello world 2021!");
62+
}
63+
64+
// @RequestMapping("/trace/id")
65+
// String traceId() {
66+
// log.info(tracer.toString());
67+
// return tracer.toString();
68+
// }
69+
//
70+
// @RequestMapping("/span/id")
71+
// String spanId() {
72+
// Span span = tracer.currentSpan();
73+
// log.info(span.toString());
74+
// return span.toString();
75+
// }
76+
77+
@Bean
78+
public EchoService echoService() {
79+
return new EchoService();
80+
}
81+
82+
public static void main(String[] args) {
83+
new SpringApplicationBuilder(ServiceProviderApplication.class)
84+
.run("--spring.config.additional-location=classpath:/META-INF/service-provider.yaml");
85+
}
86+
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
spring:
2+
application:
3+
name: service-client
4+
5+
# default disable all
6+
cloud:
7+
nacos:
8+
discovery:
9+
enabled: false
10+
register-enabled: false
11+
zookeeper:
12+
enabled: false
13+
consul:
14+
enabled: false
15+
16+
discovery:
17+
client:
18+
simple:
19+
instances:
20+
service-provider[0]:
21+
instanceId: service-provider-1
22+
serviceId: service-provider
23+
host: 127.0.0.1
24+
port: 8080
25+
26+
27+
eureka:
28+
client:
29+
enabled: false
30+
31+
management:
32+
health:
33+
elasticsearch:
34+
enabled: false
35+
36+
server:
37+
port: 9090
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
spring:
2+
application:
3+
name: service-provider
4+
5+
# default disable all
6+
cloud:
7+
nacos:
8+
discovery:
9+
enabled: false
10+
register-enabled: false
11+
zookeeper:
12+
enabled: false
13+
consul:
14+
enabled: false
15+
16+
17+
eureka:
18+
client:
19+
enabled: false
20+
21+
management:
22+
health:
23+
elasticsearch:
24+
enabled: false
25+

0 commit comments

Comments
 (0)