当前位置:网站首页>Microservice development steps (Nacos)

Microservice development steps (Nacos)

2022-07-01 14:30:00 Whales are seen when the sea is blue-

 Insert picture description here
Other module Introduced in mall-common modular
Every Module They are connected to a database

1. Turn on nacos service

 Insert picture description here

2. stay mall-commom Introduce dependency in

<!--1. First step : Registration discovery of service -->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

3. in service ( Not common modular ) Of application.yml Configuration in file nacos The address of

spring:
  # To configure Nacos Server Address and write application.name
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: mall-member
    # Other services are configured separately name: mall-coupon、mall-member、mall-order、mall-product、mall-ware

4. Giving a Module Configure annotations on the startup class in @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class MallMemberApplication {
    

    public static void main(String[] args) {
    
        SpringApplication.run(MallMemberApplication.class, args);
    }

}

Through the above steps, you can nacos See the registered module

 Insert picture description here

========================================================================
The next step is to use remote invocation to call methods from one service to another :Fegin Declarative remote call
explain :mall-member The service wants to call mall-coupon service , Then in mall-member Add something to the service

5. stay mall-member Of pom.xml Introduce dependency in asking price

<dependency>
	 <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>

6. stay mall-coupon Write a method to prepare mall-member call

@RequestMapping("/member/list")
public R membercoupons() {
    
    CouponEntity couponEntity = new CouponEntity();
    couponEntity.setCouponName(" full 100 reduce 10");
    return R.ok().put("coupons", Arrays.asList(couponEntity));
}

7. Write an interface , tell SpringCloud This interface needs to call the remote service

stay mall-member Write an interface in the module

@FeignClient("mall-coupon") // The name of the service called 
public interface CouponFeginService {
    

	// Which interface is used to write which 
    @RequestMapping("/coupon/coupon/member/list") // It says here that mall-coupon The complete request path of the interface in 
    public R membercoupons();
}

8. Turn on the function of remote call

 Insert picture description here

@EnableFeignClients(basePackages = {
    "com.eternal.mall.member.fegin"})
@SpringBootApplication
@EnableDiscoveryClient
public class MallMemberApplication {
    

    public static void main(String[] args) {
    
        SpringApplication.run(MallMemberApplication.class, args);
    }

}

9. Start using

 Insert picture description here

The above is the use of nacos Use as a registry

===================================================

The following uses nacos Manage the configuration information of each service as the configuration center

10. introduce Nacos Config Starter

Because every module can use , So put it directly on mall-common Module

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

11. Under each module that needs to be used /src/main/resources/bootstrap.properties Configuration in profile Nacos Config Metadata

spring.application.name=mall-coupon # Change according to the module name 
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

12. Add a current service to the configuration center .properties Configuration file for

The default rules : Application name .properties
You can add any configuration data
 Insert picture description here

13. obtain nacos Profile data created in

 Insert picture description here

原网站

版权声明
本文为[Whales are seen when the sea is blue-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011420263399.html

随机推荐