SpringBoot - integrate Dubbo+Zookeeper

When integrating dubbo and zookeeper, I encountered all kinds of messy errors. Share it

version control

  • SpringBoot2.5.2

1, Add dependency and log configuration

1. Rely on

		<!--zookeeper client-->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeeper</groupId>
					<artifactId>zookeeper</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--dubbo Custom launcher-->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.8</version>
		</dependency>
		<!--curator provide zookeeper connect-->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.12.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.12.0</version>
		</dependency>

Rely on the crazy God to report errors, and then find a lot of blogs. Finally, it's good to report errors all the time
Either rely on error reporting or start error reporting

be careful:

  1. Curator dependency must be added. The current version of zookeeper needs to be connected through curator
  2. zkclient must exclude zookeeper, otherwise an error will be reported

2. Log configuration

dubbo and zookeeper use log4j as logs by default, and create log4j in the resources directory Properties

log4j.rootLogger=info,ServerDailyRollingFile,stdout

log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=var/log/chinaamc/management-cockpit2.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p [%c] - %m%n
log4j.appender.ServerDailyRollingFile.Append=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d yyyy-MM-dd HH:mm:ss %p [%c] %m%n

2, Dubbo architecture


Call relation description

  • The service container is responsible for starting, loading and running the service provider.
  • When a service provider starts, it registers its services with the registry.
  • When the service consumer starts, it subscribes to the service it needs from the registry.
  • The registry returns the service provider address list to the consumer. If there is any change, the registry will push the change data to the consumer based on the long connection.
  • From the provider address list, the service consumer selects one provider to call based on the soft load balancing algorithm. If the call fails, it selects another provider to call.
  • Service consumers and providers accumulate call times and call times in memory, and regularly send statistical data to the monitoring center every minute.

Dubbo architecture has the following characteristics: connectivity, robustness, scalability and upgrading to the future architecture.

There are many registration centers, but we use zookeeper here, which is also commonly used. Because services need to register services in the registration center and find required services through the network, the response is generally very slow

Dubbo official website: architecture description link

3, Provider services

directory structure

1. Write service interface

@DubboService//Publishing services
@Component//Put in container
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "Follow madness Java";
    }
}

2. Provider service configuration

The service provider needs to scan the services provided and submit them to the registration center

dubbo:
  application:
    ##1. Current service name
    name:  provider-service
  registry:
    # TEST environment
    ##2. Address of Registration Center
    address: zookeeper://127.0.0.1:2181
    protocol: zookeeper
    check: false
  ##3. Scan the services to be registered
  scan:
    base-packages: com.chime.prodiver.service
server:
  port: 8001

Note: the ip address / / should not be written as/

3. Dubbo admin monitoring view service

  1. Open zookeeper service: zkserver cmd
  2. java -jar xxx.jar: run Dubbo admin to monitor the jar package
  3. Then you can visit localhost:7001 to view the service (this service needs to be deployed and run the main program)

Service successfully registered

4, Consumer services

Project directory

1. Write methods for consumers to use providers

@Service
public class BuyTicketService {
	 //The specified service can be referenced remotely, which can be matched through the full class name. You can write a fake interface to the provider by imitating the service structure of the provider's service
	 //What really works is the interface of the provider service
    @DubboReference
    TicketService ticketService;

    public String buyTicket(){
        return "I bought it at the registry"+ticketService.getTicket();
    }
}

2. Disposition

server.port=8002
#service name
dubbo.application.name=consumer-service
#Address of Registration Center
dubbo.registry.address=zookeeper://127.0.0.1:2181

3. Run the main program to view the service

4. Test class test remote call

@SpringBootTest
class ConsumerServiceApplicationTests {
	@Autowired
	BuyTicketService buyTicketService;
	@Test
	void contextLoads() {
		System.out.println(buyTicketService.buyTicket());
	}

}

Two projects call the service directly

Keywords: Spring Framework

Added by NoPHPPhD on Tue, 25 Jan 2022 05:48:17 +0200