premise
Recent start-up companies the project team based on business needs, the development of a new micro-services, taking into account the components chosen must be mainstream, community activists, ecological improvement and to facilitate the migration to the cloud finest factors, the introduction of SOFAStack
family bucket. Micro service development there, a very important feature is the service discovery and registration, it took some time to do a SOFABoot
, SOFARpc
combine Nacos
to achieve service discovery micro-example of registration and remote call.
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-szbsGPln-1577958719897) (sfsn-1.png)]
Reliance version stepped pit
I spent some time trying SOFABoot
, SOFARpc
in conjunction with Nacos
version-dependent relationship between the client, when completed as of this writing (2020-01-01), sofaboot-dependencies
the latest version 3.2.1
, corresponds to SOFABoot-3.2.1
, SOFARpc-5.6.3
and SpringBoot-2.1.x.RELEASE
. In both the latest version of the project, no matter what version introduced nacos-clinet
, there is no way to Nacos-Server
information registration service. In this regard, I once from the Issues
find relevant content inside, temporarily to no avail, so the sample project to share the analysis of community chiefs, if there is a solution, will update this blog. Try out version combinations available are:
sofaboot-dependencies:3.2.0
spring-boot-dependencies:2.1.0.RELEASE
nacos-api:0.6.0
withnacos-client:0.6.0
Introducing the following dependence:
<properties>
<sofa.boot.version>3.2.0</sofa.boot.version>
<spring.boot.version>2.1.0.RELEASE</spring.boot.version>
<nacos.version>0.6.0</nacos.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>${sofa.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>healthcheck-sofa-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-api</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
Write the service provider and the service consumer codes
There is a premise, a need to start Nacos-Server
the sake of convenience, you can use the stand-alone model for local start, then the service address is registered http://127.0.0.1:8848
. Configuration example items are as follows:
├─src
│ └─main
│ ├─java
│ │ └─club
│ │ └─throwable
│ │ ├─client
│ │ │ ClientApplication.java
│ │ │
│ │ ├─contract
│ │ │ HelloService.java
│ │ │
│ │ └─server
│ │ DefaultHelloService.java
│ │ ServerApplication.java
│ │
│ └─resources
│ application-client.properties
│ application-server.properties
Which contract
is a contract package that can be provided to the client and server use, client
the package inside to write client ( comsumer
) code, and server
the package inside to write server-side ( provider
) code.
Compact Interface HelloService
is simple:
public interface HelloService {
String sayHello(String name);
}
Service providers need to implement this interface, implementation class is DefaultHelloService
:
@Service
@SofaService(interfaceType = HelloService.class, bindings = {
@SofaServiceBinding(bindingType = "bolt")
})
public class DefaultHelloService implements HelloService {
@Override
public String sayHello(String name) {
return String.format("%s say hello!", name);
}
}
Here service agreement binding type is used bolt
, it is the official example of the recommended protocol, of course dubbo
, http
and so on, can be mixed configurations. Then start writing service provider categories ServerApplication
:
@SpringBootApplication(scanBasePackages = {"club.throwable.server", "club.throwable.contract"})
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
The service provider application configuration file application-server.properties
as follows:
spring.application.name=sofa-rpc-provider
server.port=9092
# 用Nacos做注册中心
com.alipay.sofa.rpc.registry-address=nacos://127.0.0.1:8848
Use spring.profiles.active=server
start ServerApplication
, after a successful start to open the browser Nacos-Console
:
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-elaaCtia-1577958719898) (sfsn-2.png)]
We see the current sofa-rpc-provider
service has been successfully registered Nacos-Server
. Then write client code, for convenience, all of the code written in the startup class ClientApplication
:
@Slf4j
@SpringBootApplication(scanBasePackages = {"club.throwable.client", "club.throwable.contract"})
public class ClientApplication implements CommandLineRunner {
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"))
private HelloService helloService;
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
log.info("调用HelloService#sayHello(),结果:{}", helloService.sayHello("throwable"));
}
}
Service consumer profile application-client.properties
is as follows:
spring.application.name=sofa-rpc-consumer
server.port=9091
# 用Nacos做注册中心
com.alipay.sofa.rpc.registry-address=nacos://127.0.0.1:8848
Use spring.profiles.active=client
start ClientApplication
, start the console output after completion:
2020-01-02 17:07:58.782 INFO 2900 --- [main] club.throwable.client.ClientApplication : 调用HelloService#sayHello(),结果:throwable say hello!
The basic principle is as follows:
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-pvKYnGOy-1577958719898) (sfsn-3.png)]
summary
SOFABoot
, SOFARpc
The bottom is dependent on Spring
container, you can follow the SpringBoot
version iterative upgrade, use the underlying communications Netty
, guaranteed in performance, but also truly compatible HTTP
, , Dubbo
(behindService Mesh
will be Service Mesh
carried out as a compatible communications protocol) protocol and so on, for developers, relatively Friendly, low-cost study, to be truly out of the box to add a small amount of configuration can be used. In addition to the current found to be dependent version of the problem, there is no big pit, try some fresh feeling quite good.
Sample project:
Description link
- Github Page] [personal blog: http: //throwable.club/2020/01/01/sofa-boot-nacos-get-start
- Coding Page] [personal blog: http: //throwable.coding.me/2020/01/01/sofa-boot-nacos-get-start
(Herein End c-1-d ea-20200101)