This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022.
Learn with me: distributed link tracking ELK early build with me: distributed link tracking ELK log combat
1 introduction
In the last article, we carried out the specific configuration of the ELK environment, and successfully cleaned the data through ELK and stored it in ES.
Now that you have the environment, the project needs to have it. In this session, we will focus on building distributed projects as data sources.
The only tracks used were Dubbo and SpringCloud. Given that some components of SpringCloud may not be open source in the future. So the decision was made to use Alibaba’s SpringCloudAlibaba as the cornerstone and Nacos as the service discovery and configuration.
2 Nacos
2.1 installation Nacos
It is recommended to install nacos2.0. Compared with 1.x, 2.x mainly adds long connection and has better performance. Nacos also supports migration of Dubbo Zookeeper. Details can be found on the official website: nacOS
2.2 Creating a Namespace
In a real project, there will be at least two environments: a test environment and a production environment.
For this reason, create a namespace named dev, although enterprise projects may also have namespaces for pre and PROd.
2.3 Creating a Configuration
Under the dev namespace, we create the configuration for the provider. Note: Similar to Apollo, it is best to have a file suffix on our side.
3 Service Configuration
3.1 Adding a parent module dependency
The parent module adds dependencies using the latest version of Cloud-Alibaba :2.2.7.RELEASE.
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> < artifactId > spring - the cloud - alibaba - dependencies < / artifactId > < version > 2.2.7. RELEASE < / version > < type > pom < type > <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code
3.2 Adding submodule dependencies
<dependencies> <! <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>Copy the code
3.3 Adding a Configuration File
Since the Nacos environment configuration does not support application.yml, we need to create bootstrap.yml and bootstrap-dev.yml under Resources
3.3.1 the bootstrap. Yml
server:
port: 8081
spring:
application:
name: provider
profiles:
active: dev
Copy the code
3.3.2 rainfall distribution on 10-12 bootstrap – dev. Yml
Spring: Cloud: nacos: config: namespace: dceedfe4-0041-41C5-be76-ed4c3689634d 127.0.0.1:8848 file - the extension: yamlCopy the code
3.4 create RestController
Attention please!! To keep Nacos updated dynamically, we need to add the @refreshScope annotation to the Bean.
So to reduce the amount of work, try to create Config classes to reduce the configuration.
@RestController @RefreshScope public class ProviderController { @Value("${useLocalCache1}") private String useLocalCache; @GetMapping(value = "/get") public String get() { return useLocalCache; }}Copy the code
3.5 practice
First call:
Call after modification and discover that the value is dynamically updated
4 Service Discovery
4.1 Service Producers
4.1.1 Adding a Dependency
<dependency> <groupId>com.alibaba. Cloud </groupId> </groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>Copy the code
4.1.2 Adding the Service Discovery Configuration
Spring: Cloud: nacos: Discovery: Namespace: dceedFE4-0041-41C5-be76-ed4c3689634d server-addr: 127.0.0.1:8848Copy the code
4.1.4 Adding producer methods
@getMapping (value = "/echo/{string}") public string echo(@pathVariable String String) {log.info(" I am printing log "); return "Hello Nacos Discovery " + string; }Copy the code
4.2 Service consumers
4.2.1 Adding a Dependency
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code
4.2.2 Adding the Service Discovery Configuration
server: port: 8082 spring: application: name: service-consumer cloud: nacos: discovery: namespace: Dceedfe4-0041-41 c5 - be76 - ed4c3689634d server - addr: 127.0.0.1:8848Copy the code
4.2.3 Creating caller methods
@LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @RestController public class TestController { private final RestTemplate restTemplate; @Autowired public TestController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @RequestMapping(value = "/echo/{str}", Method = requestmethod.get) public String echo(@pathVariable String STR) {log.info(" start calling log "); return restTemplate.getForObject("http://provider/echo/" + str, String.class); }Copy the code
4.3 call
You can see that the service provided by the producer was successfully invoked
At the end of may
Over here, the distributed project is basically complete. We’ll see you next time